package de.graffiti.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jboss.logging.Logger;

import de.graffiti.beans.DBConnection;
import de.graffiti.interfaces.WallpaperService;

/**
 * Servlet implementation class TimeService
 */
public class FileUploadServlet extends HttpServlet
{
	private static final Logger log = Logger.getLogger(FileUploadServlet.class.getName());

	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public FileUploadServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{

		PrintWriter out = response.getWriter();

		int height = 0;
		int width = 0;
		boolean repeating = false;
		byte[] file = null;
		String titel = "";

		boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
		if (!isMultipartContent)
		{
			out.println("You are not trying to upload<br/>");
			return;
		}
		out.println("<html><head></head><body>");
		out.println("You are trying to upload<br/>");

		// Create a factory for disk-based file items
		DiskFileItemFactory factory = new DiskFileItemFactory();

		String errorMessage = "";
		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);

		// Set overall request size constraint
		upload.setSizeMax(5000000);

		// Parse the request

		try
		{
			List items = upload.parseRequest(request);

			if (items.get(0) != null)
			{
				FileItem fileItem = (FileItem) items.get(0);
			}

			Iterator iter = items.iterator();
			while (iter.hasNext())
			{
				FileItem fileItem = (FileItem) iter.next();
				boolean isFormField = fileItem.isFormField();
				if (!isFormField)
				{
					file = fileItem.get();
				}
				if (fileItem.getFieldName().equals("repeating"))
					repeating = true;
				if (fileItem.getFieldName().equals("title") && titel != null)
					titel = fileItem.getString();
				try
				{
					if (fileItem.getFieldName().equals("height"))
						height = Integer.parseInt(fileItem.getString());
					if (fileItem.getFieldName().equals("width"))
						width = Integer.parseInt(fileItem.getString());

				} catch (NumberFormatException e)
				{
					height = 100;
					width = 100;
				}
			}
			DBConnection db = new DBConnection();
			WallpaperService wpservice = (WallpaperService) db.getService("WallpaperServiceBean/remote", WallpaperService.class);
			boolean uploaded = wpservice.saveWallpaper("anonym", titel, file, "image/jpg", repeating, width, height);
			if (uploaded)
				out.println("sucess");
			else
				out.println("failed<br>");
			out.println("<a href=\"jsf/login.jsf\">back</a>");
			out.println("</body></html>");
		} catch (FileUploadException e)
		{
			e.printStackTrace();

		}

	}

}
